<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改课程')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-course-edit" th:object="${vendorCourse}">
            <input name="id" th:field="*{id}" type="hidden">
            <input name="coursePackageId" th:field="*{coursePackageId}" class="form-control" type="hidden" >

            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select  id="typeName" name="type" th:field="*{type}" class="form-control m-b" th:with="type=${@dict.getType('course_package_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" ></option>
                    </select>
                </div>
            </div>
            <div class="form-group" id="indexInPackage_1">
                <label class="col-sm-3 control-label">显示顺序：</label>
                <div class="col-sm-8">
                    <input id="indexInPackage"  name="indexInPackage" th:field="*{indexInPackage}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">月龄 低限：</label>
                <div class="col-sm-8">
                    <input id="ageLow" name="ageLow" th:field="*{ageLow}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">月龄 高限：</label>
                <div class="col-sm-8">
                    <input id="ageHigh" name="ageHigh" th:field="*{ageHigh}" class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">时长：</label>
                <div class="col-sm-8">
                    <input id="durations" name="durations"  th:field="*{durations}"  class="form-control" type="text">
                </div>
            </div>

            <div class="form-group " id="type_1_1">
                <label class="col-sm-3 control-label">周次：</label>
                <div class="col-sm-8">
                    <select id="type_1_1_val" name="type_1_1_val" th:field="*{type_1_1_val}" class="form-control m-b"
                            th:with="type=${@dict.getType('tc_week')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group " style="display:none;"  id="type_1_1_2">
                <label class="col-sm-3 control-label">周次：</label>
                <div class="col-sm-8">
                    <!--                家庭-->
                    <select id="type_1_1_2_val"  th:field="*{type_1_1_val}" class="form-control m-b"
                            th:with="type=${@dict.getType('tc_family_week')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group "  style="display:none;" id="type_1_1_3">
                <label class="col-sm-3 control-label">周次：</label>
                <!--            群体-->
                <div class="col-sm-8">
                    <select id="type_1_1_3_val"  th:field="*{type_1_1_val}"  class="form-control m-b"
                            th:with="type=${@dict.getType('tc_group_week')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group " id="type_2_1">
                <label class="col-sm-3 control-label">面向：</label>
                <div class="col-sm-8">
                    <select id="type_2_1_val"  th:field="*{type_2_1_val}" name="type_2_1_val" class="form-control m-b"
                            th:with="type=${@dict.getType('tc_face')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group "  style="display:none;" id="type_2_2">
                <label class="col-sm-3 control-label">面向：</label>
                <div class="col-sm-8">
                    <select id="type_2_2_val"  th:field="*{type_2_1_val}" class="form-control m-b"
                            th:with="type=${@dict.getType('tc_family_face')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group " id="type_1_2">
                <label class="col-sm-3 control-label">教材：</label>
                <div class="col-sm-8">
                    <input  id="type_1_2_val" th:field="*{type_1_2_val}"class="form-control" type="text">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <input name="title" th:field="*{title}" class="form-control" type="text" required>
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label is-required">内容：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input type="hidden" class="form-control" th:field="*{content}">-->
<!--                    <div class="summernote" id="content"></div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="comment" th:field="*{comment}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group hidden">
                <label class="col-sm-3 control-label is-required">详情：</label>
                <div class="col-sm-8">
                    <input id="details" name="details" th:field="*{details}"class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/course";
        $("#form-course-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var type =  $("#typeName").val();
                var details = "";
                debugger;
                //机构端
                if (type == 0 ){
                    details = "[{\"label\":\"周次\",\"value\":\""+$("#type_1_1_val").val()+"\"}," +
                        "{\"label\":\"教材\",\"value\":\""+$("#type_1_2_val").val()+"\"}," +
                        "{\"label\":\"面向\",\"value\":\""+$("#type_2_1_val").val()+"\"}," +
                        "{\"label\":\"课堂时间\",\"value\":\""+$("#durations").val()+"\"}]" ;
                }
                //家庭端
                if (type == 1 ){
                    details = "[{\"label\":\"周次\",\"value\":\"" + $("#type_1_1_2_val").val()+"\"}," +
                        "{\"label\":\"月龄\",\"value\":\""+$("#ageLow").val()+"-"+$("#ageHigh").val()+"\"}," +
                        "{\"label\":\"面向\",\"value\":\""+$("#type_2_2_val").val()+"\"}," +
                        "{\"label\":\"教材\",\"value\":\"" + $("#type_1_2_val").val() + "\"}," +
                        "{\"label\":\"课堂时间\",\"value\":\""+$("#durations").val()+"\"}]" ;
                }
                //小群体课
                if (type == 2 ){
                    details = "[{\"label\":\"周次\",\"value\":\""+$("#type_1_1_3_val").val()+"\"}," +
                        "{\"label\":\"教材\",\"value\":\""+$("#type_1_2_val").val()+"\"}," +
                        "{\"label\":\"课堂时间\",\"value\":\""+$("#durations").val()+"\"}]" ;
                }
                $("#details").val(details);
                $.operate.save(prefix + "/edit", $('#form-course-edit').serialize());
            }
        }

        $("input[name='lastUpdated']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })

            getType();
        });

        $("#typeName").change(function(){
            getType();
        });

        function getType() {
            var type =  $("#typeName").val();
            if (type == 0) {
                $("#type_1_1").attr("style", "display:block;");//显示div
                $("#type_1_2").attr("style", "display:block;");//显示div
                $("#type_2_1").attr("style", "display:block;");//显示div

                $("#type_2_2").attr("style", "display:none;");//隐藏div
                //家庭端周次
                $("#type_1_1_2").attr("style", "display:none;");//隐藏div
                $("#type_1_1_3").attr("style", "display:none;");//隐藏div


            }
            //家庭端
            if (type == 1) {

                $("#type_1_1").attr("style", "display:none;");//隐藏div
                $("#type_2_1").attr("style", "display:none;");//隐藏div
                $("#type_1_1_3").attr("style", "display:none;");//隐藏div

                $("#type_2_2").attr("style", "display:block;");//隐藏div
                $("#indexInPackage_1").attr("style", "display:block;");//显示div
                $("#type_1_1_2").attr("style", "display:block;");//显示div

            }
            //小群体课
            if (type == 2) {
                $("#type_1_1").attr("style", "display:block;");//显示div
                $("#type_1_2").attr("style", "display:block;");//显示div
                $("#type_2_2").attr("style", "display:block;");//显示div
                $("#type_1_1_3").attr("style", "display:block;");//显示div

                $("#type_1_1").attr("style", "display:none;");//隐藏div
                $("#type_1_1_2").attr("style", "display:none;");//隐藏div
                $("#type_2_1").attr("style", "display:none;");//隐藏div
                $("#type_2_2").attr("style", "display:none;");//隐藏div
            }
        }
    </script>
</body>
</html>